﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using System.Web;
using Database;
using HtmlAgilityPack;
namespace Server.Models
{
    public class GalaxyCinema
    {
        public static List<string> LayURL(int type)
        {
            var list = new List<string>();
            HtmlDocument ds = new HtmlDocument();
            HtmlWeb hw = new HtmlWeb();
            if (type == 0)//lay phim dang chieu
            {
                ds = hw.Load("https://www.galaxycine.vn/vi/phim/nowshowing");
            }
            else if (type == 1) // lay phim sap chieu
            {
                ds = hw.Load("https://www.galaxycine.vn/vi/phim/comingsoon");
            }
            var d = ds.DocumentNode.SelectNodes("//div[@class='rel']");

            foreach (HtmlNode link in d)
            {

                HtmlNodeCollection ls = link.ChildNodes;
                HtmlNode l = ls[1];
                var value = "";
                if (l.Attributes.Count > 0)
                    value = l.Attributes[0].Value;

                if (value.IndexOf("/vi/thong-tin-phim") == 0)
                {
                    list.Add("https://www.galaxycine.vn" + value);
                }
            }
            return list;
        }
        public static Galaxy ThongTinFilm(string url)
        {
            HtmlDocument ds = new HtmlDocument();
            HtmlWeb hw = new HtmlWeb();
            ds = hw.Load(url);

            var d = ds.DocumentNode.SelectNodes("//span[@class='content']");
            var film = new Galaxy();
            film.Ten = (ds.DocumentNode.SelectSingleNode("//div[@class='title-article']")).InnerHtml;
            film.Khoichieu = d[0].InnerHtml;
            film.Theloai = d[1].InnerHtml;
            film.Phienban = d[5].InnerHtml;
            film.Nuocsanxuat = d[7].InnerHtml;
            film.Cumrap = d[8].InnerHtml;
            //   var dsd = (ds.DocumentNode.SelectSingleNode("//img[@class='img-movie-detail']"));
            film.Mota = (ds.DocumentNode.SelectSingleNode("//div[@class='content']")).ChildNodes[0].InnerHtml;
            film.Trailer = (ds.DocumentNode.SelectSingleNode("//input[@id='txtTrailerDetail']")).Attributes[2].Value;
            film.Url = url;
            film.Hinhanh = "https://www.galaxycine.vn" + (ds.DocumentNode.SelectSingleNode("//img[@class='img-movie-detail']")).Attributes[0].Value;

            return film;
        }

        public static List<Galaxy> DsThongTinFilm(List<string> urllst)
        {
            var list = new List<Galaxy>();
            foreach (var url in urllst)
            {
                var film = ThongTinFilm(url);
                list.Add(film);

            }

            return list;
        }
        
    }
}